<?php

namespace pmvc\view;

use pmvc\ViewHandler;
pmvc_classpath_require("/smarty/Smarty.class.php");
use Smarty;

class SmartyViewHandler 
    implements ViewHandler {
    
    var $smarty;
    var $file;
    
    /**
     * Creates this shit
     */
    public function configure($conf) {
    	
		// create needed directories
		if (!file_exists($conf["compileDir"]) 
			&& !mkdir($conf["compileDir"], 755, TRUE)) {
			pmvc_logerror("Unable to create compile dir: {0}", $conf["compileDir"]);
		}
		if (!file_exists($conf["configDir"]) 
			&& !mkdir($conf["configDir"], 755, TRUE)) {
			pmvc_logerror("Unable to create config dir: {0}", $conf["configDir"]);
		}
		if (!file_exists($conf["cacheDir"]) 
			&& !mkdir($conf["cacheDir"], 755, TRUE)) {
			pmvc_logerror("Unable to create cache dir: {0}", $conf["cacheDir"]);
		}
		if (array_key_exists("hookFile", $conf)) {
			pmvc_classpath_require($conf["hookFile"]);
		}

        // create
        $this->smarty = new Smarty();
        
        // configure
        $this->smarty->template_dir = $conf["templateDir"];
        $this->smarty->compile_dir  = $conf["compileDir"];
        $this->smarty->config_dir   = $conf["configDir"];
        $this->smarty->cache_dir    = $conf["cacheDir"];
        $this->smarty->caching	    = strtolower($conf["useCache"])=="true";
        $this->smarty->use_sub_dirs = TRUE;
        
        // set variables
        $this->smarty->assign(Array(
            "request"	    => $_REQUEST,
            "post"	        => $_POST,
            "get"	        => $_GET,
            "session"       => $_SESSION,
            "server"	    => $_SERVER
        ));
        
        // bring in the functions 
        pmvc_classpath_require("/pmvc/view/smarty_functions.php");
        
        // retgister functions
        $this->smarty->register_function(
        	"pmvc_getContextUrl", "smarty_pmvc_getContextUrl", false);
        $this->smarty->register_function(
        	"pmvc_getRequestedUrl", "smarty_pmvc_getRequestedUrl", false);
        $this->smarty->register_function(
        	"pmvc_getAttribute", "smarty_pmvc_getAttribute", false);
        $this->smarty->register_function(
        	"pmvc_formInput", "smarty_pmvc_formInput", false);
        $this->smarty->register_function(
        	"pmvc_formTextArea", "smarty_pmvc_formTextArea", false);
        $this->smarty->register_function(
        	"pmvc_formInputPassword", "smarty_pmvc_formInputPassowrd", false);
        $this->smarty->register_function(
        	"pmvc_displayFormGlobalErrors", "smarty_pmvc_displayFormGlobalErrors", false);
        $this->smarty->register_function(
        	"pmvc_displayFormFieldErrors", "smarty_pmvc_displayFormFieldErrors", false);
        $this->smarty->register_function(
        	"pmvc_niceDate", "smarty_pmvc_niceDate", false);
			
		// register blocks
		$this->smarty->register_block('pmvc_dynamic', 'smarty_pmvc_block_dynamic', false);
		
		// let something hook
		if (function_exists("pmvc_smarty_hook")) {
			pmvc_smarty_hook($this->smarty);
		}
    }
    
    public function renderView($mav) {
        
        // put the model into the template
        $this->smarty->assign($mav->getModel());
        
        // render the view
        $this->smarty->display($mav->getViewName());
    }
    
}

?>
